package com.uflo.test.listener;

import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.process.listener.TaskListener;
import com.bstek.uflo.process.node.TaskNode;
import com.uflo.test.model.BaoXiao;
import com.uflo.test.model.Orderflow;

@Component("test.demoTaskListener")
public class DempTaskListener implements TaskListener {

	public boolean beforeTaskCreate(Context arg0, ProcessInstance arg1, TaskNode arg2) {
		System.out.println("beforeTaskCreate...");
		return false;
	}

	public void onTaskComplete(Context arg0, Task arg1) {
		System.out.println("onTaskComplete...");

	}

	public void onTaskCreate(Context context, Task task) {
		Session session = context.getSession();
		
		try {
			String businessId = task.getBusinessId();
			
			BaoXiao baoXiao = (BaoXiao) session.get(BaoXiao.class, businessId);
			
			Orderflow orderflow = new Orderflow();
			orderflow.setId(task.getId() + "/" + baoXiao.getId());
			
			orderflow.setTaskId(task.getId());
			orderflow.setTaskName(task.getTaskName());
			orderflow.setAssignee(task.getAssignee());
			orderflow.setProcessInstanceId(task.getProcessInstanceId());
			orderflow.setOpinion(task.getOpinion());
			orderflow.setState(task.getState());
			orderflow.setUrl(task.getUrl());
			
			orderflow.setBusinessId(baoXiao.getId());
			orderflow.setName(baoXiao.getName());
			orderflow.setType(baoXiao.getType());
			orderflow.setDesc(baoXiao.getDesc());
			session.saveOrUpdate(orderflow);
		} finally {
			session.flush();
			//session.close();
		}
		
	}

}
